# coding=utf-8
"""
author: jamon
"""
import sys
from typing import Optional, List

import pygame
from pygame.event import Event
from pygame.font import Font
from pygame.surface import Surface

from client.config import ClientConfig
from client.views.base import ScreenBase, register_screen
from client.views.scene_mgr import scene_mgr
from obespoir.share.espoir_log import global_log as logger

color_white = (255, 255, 255)
color_gray = (192, 192, 192)
_need_reload = True


@register_screen("switch_gate")
class SwitchGateScreen(ScreenBase):

    def __init__(self, screen: Surface):
        super(SwitchGateScreen, self).__init__(screen=screen)
        self.config = ClientConfig()
        self.background_img: Surface = pygame.image.load(self.config.background_image_path)

        width = self._screen.get_width()
        height = self._screen.get_height()
        self.font: Font = pygame.font.SysFont("Arial", width // 20)
        logo_img = pygame.image.load(self.config.logo_image)
        self.logo_img = pygame.transform.scale(logo_img, (446, 70))
        self.logo_rect = logo_img.get_rect()
        self.logo_rect.centerx, self.logo_rect.centery = width / 2, height // 4

        # 游戏加载进度条
        self.gamebar = pygame.image.load(self.config.game_bar).convert_alpha()
        self.gamebar_rect = self.gamebar.get_rect()
        self.gamebar_rect.centerx, self.gamebar_rect.centery = width / 2, height / 1.4

        self.tank_cursor = pygame.image.load(self.config.tank_cursor_image).\
            convert_alpha().subsurface((0, 144), (48, 48))
        self.tank_rect = self.tank_cursor.get_rect()
        self.tank_rect.left = self.gamebar_rect.left
        self.tank_rect.centery = self.gamebar_rect.centery
        # 加载所需时间
        self.load_time_left = (self.gamebar_rect.right - self.tank_rect.right) // 2 + 8

        self.is_multi_mode: bool = False
        self.gate_id: int = 1

        self.font_render: Optional[Surface] = None
        self.font_rect: Optional[pygame.Rect] = None

    def init_screen(self, mode: bool, gate_id: int, *args, **kwargs):
        self.is_multi_mode = mode
        self.gate_id = gate_id

        width = self._screen.get_width()
        height = self._screen.get_height()
        # 游戏加载提示
        self.font_render = self.font.render('Loading game data, You will enter Level-%s' % gate_id, True, color_white)
        self.font_rect = self.font_render.get_rect()
        self.font_rect.centerx, self.font_rect.centery = width / 2, height / 2

    async def update(self, events: List[Event] = None, **kwargs):
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        if self.load_time_left <= 0:
            self.switch_other_screen()
        else:
            self._screen.blit(self.background_img, (0, 0))
            self._screen.blit(self.logo_img, self.logo_rect)
            self._screen.blit(self.font_render, self.font_rect)
            self._screen.blit(self.gamebar, self.gamebar_rect)
            self._screen.blit(self.tank_cursor, self.tank_rect)
            pygame.draw.rect(
                self._screen, color_gray,
                (self.gamebar_rect.left + 8, self.gamebar_rect.top + 8,
                 self.tank_rect.left - self.gamebar_rect.left - 8,
                 self.tank_rect.bottom - self.gamebar_rect.top - 16)
            )
            self.tank_rect.left += 1
            self.load_time_left -= 1

    def switch_other_screen(self):
        if self.is_changing_scene:
            return
        self.is_changing_scene = True
        new_screen = "battle"
        logger.info("start", new_screen=new_screen, mode=self.is_multi_mode,
                    new_gate_id=self.gate_id)
        scene_mgr.switch_to_scene(new_screen, mode=self.is_multi_mode, gate_id=self.gate_id)
